<template>
	<!-- 选择目标路径 -->
	<n-modal
		title="选择目标路径"
    v-model:show="modelVisible"
    preset="dialog"
		:close-on-click-modal="false"
		width="580px"
    :on-after-enter="initFileFolderTree"
    @close="handleDialogClose"
	>
		<n-form :model="form" :rules="formRules" ref="moveFormRef"
			label-width="100px"
			label-position="top"
		>
			<n-form-item label="目标路径" path="filePath">
				<n-input
					v-model:value ="form.filePath"
					placeholder="目标路径"
          readonly="true"
				></n-input>
      </n-form-item>
        <n-spin :show="loading">
          <n-tree
              block-line
              :data="fileFolderTree"
              :default-checked-keys="defaultExpandedKeys"
              key-field="id"
              label-field="label"
              :on-update:selected-keys="handleNodeClick"
              selectable
          />
        </n-spin>

      <div style="display: flex; justify-content: flex-end;margin-top: 10px;" >
        <n-button @click="handleDialogClose" style="margin-right: 10px;">
          取消
        </n-button>
        <n-button type="primary" @click="handleDialogSure" :loading="sureBtnLoading">
          确定
        </n-button>
      </div>
		</n-form>
	</n-modal>
</template>

<script lang="ts" setup>
import {fileController} from '@/api/file/fileController'
import {useMessage} from 'naive-ui'
import {ref} from "vue";
//@ts-ignore
import Bus from '@/components/file/box/GlobalUploader/utils/bus.js'

const message = useMessage()
const moveFormRef = ref(null)
const modelVisible = ref(false)
const sureBtnLoading = ref(false)


const loading = ref(false)
/**文件树**/
const fileFolderTree = ref([])
/**当前展开的目录**/
const defaultExpandedKeys = ref([])



// 是否为批量删除：true - 批量删除，false - 单文件删除
const isBatchOperation = ref(false)
// 文件信息，批量时为数组、单文件时为对象
const fileInfo:any = ref([])
// 删除模式：1-删除到回收站 2-彻底删除
const deleteMode = ref(0)

const handleMoveFileShow = (isBatchOperation_:boolean , fileInfo_ :any , deleteMode_:number) => {
  form.value.filePath = ''
  fileFolderTree.value = []
  isBatchOperation.value = isBatchOperation_
  deleteMode.value = deleteMode_
  fileInfo.value = fileInfo_
  modelVisible.value = true
}



const form = ref({
  filePath: ''
})

/**
 * 校验规则，对文件名进行校验
 */
const formRules = {
  filePath: [
    { required: true, message: '请输入目标路径', trigger: 'blur' }
  ]
}




defineExpose({
  handleMoveFileShow
})


/**
 * 取消按钮点击事件 & 对话框关闭的回调
 * @description 关闭对话框，重置表单
 */
const handleDialogClose= () => {
  modelVisible.value = false
}


/**
 * 确定按钮点击事件
 * @description 校验表单，校验通过后调用新建文件夹接口
 */
const handleDialogSure = () => {
  sureBtnLoading.value = true
  console.log(fileInfo.value)
  if(isBatchOperation.value){
    let data = {
      filePath: form.value.filePath,
      userFileIds: fileInfo.value
          .map((item:any) => {
            return item.userFileId
          })
          .join(',')
    }
    fileController.batchMoveFile(data).then((res)=>{
      sureBtnLoading.value = false
      if(res.code === 200){
        message.success('移动成功')
        modelVisible.value = false
        fileInfo.value = []
        form.value.filePath = ''
        Bus.emit('refreshFileList')
      }else {
        message.error(res.msg)
      }
    }).catch(()=>{
      sureBtnLoading.value = false
    })
  }else {
    // 单文件移动
    let data = {
      userFileId: fileInfo.value.userFileId,
      filePath: form.value.filePath
    }
    fileController.moveFile(data).then((res)=>{
      sureBtnLoading.value = false
      if(res.code === 200){
        message.success('移动成功')
        modelVisible.value = false
        fileInfo.value = []
        form.value.filePath = ''
        Bus.emit('refreshFileList')
      }else {
        message.error(res.msg)
      }
    }).catch(()=>{
      sureBtnLoading.value = false
    })
  }
}



/**
 * 目录树节点点击回调函数
 * @description 将当前节点中的文件夹路径传递给父组件
 *
 */
const handleNodeClick = (keys:string,options:Array<any>) => {
  form.value.filePath = options[0].filePath
}


/***
 * 初始化文件目录
 * @param id
 */
const initFileFolderTree = (id:any)=>{
  loading.value = true
  fileController.getFoldTree().then((res)=>{
    loading.value = false
    if(res.code === 200){
      fileFolderTree.value.push(res.data)
      console.log(fileFolderTree.value)
    }else {
      message.error(res.msg)
    }

  })
}
</script>
